package com.leonyr.smartipaddemo.crop;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.leonyr.smartipaddemo.R;

import java.util.Timer;
import java.util.TimerTask;

public class CountDownFragment extends DialogFragment {
    private ImageView imageView;
    private Timer timer;
    private TimerTask task;
    private int count = 3;
    private DialogInterface.OnDismissListener onDismissListener;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                switch (msg.arg1){
                    case 0:
                        task.cancel();
                        timer.cancel();
                        dismiss();
                        break;
                    case 1:
                        imageView.setBackgroundResource(R.drawable.one);
                        break;
                    case 2:
                        imageView.setBackgroundResource(R.drawable.two);
                        break;
                    case 3:
                        imageView.setBackgroundResource(R.drawable.three);
                        break;
                };
            }
        }
    };
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setCanceledOnTouchOutside(false);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.dimAmount = 0.6f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        imageView = new ImageView(getActivity());
        imageView.setBackgroundResource(R.drawable.three);
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                if (count>0){
                    count--;
                    Message msg = handler.obtainMessage();
                    msg.what = 1;
                    msg.arg1 = count;
                    handler.sendMessage(msg);
                }
            }
        };
        timer.schedule(task,1000,1000);
        return imageView;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if(onDismissListener!=null){
            onDismissListener.onDismiss(dialog);
        }
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }
}
